/**
 * 用户相关服务
 */
const util = require('../utils/util.js');
const api = require('../config/api.js');


/**
 * Promise封装wx.checkSession
 */
function checkSession() {
  return new Promise(function (resolve, reject) {
    wx.checkSession({
      success: function () {
        resolve(true);
      },
      fail: function () {
        reject(false);
      }
    })
  });
}

/**
 * Promise封装wx.login
 */
function login() {
  return new Promise(function (resolve, reject) {
    wx.login({
      success: function (res) {
        if (res.code) {
          resolve(res);
        } else {
          reject(res);
        }
      },
      fail: function (err) {
        reject(err);
      }
    });
  });
}

/**
 * 调用微信登录
 */
function loginByWeixin(userInfo) {

  return new Promise(function (resolve, reject) {
    return login().then((res) => {
      //登录远程服务器
      util.request(api.wxLogin, {
        code: res.code,
        userInfo: userInfo
      }, 'POST').then(res => {
        if (res.errno === 0) {
          //存储用户信息
          wx.setStorageSync('userInfo', res.data.userInfo);
          wx.setStorageSync('token', res.data.token);

          resolve(res);
        } else {
          reject(res);
        }
      }).catch((err) => {
        reject(err);
      });
    }).catch((err) => {
      reject(err);
    })
  });
}

/**
 * 判断用户是否登录
 */
function checkLogin() {
  return new Promise(function (resolve, reject) {
    if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
      checkSession().then(() => {
        resolve(true);
      }).catch(() => {
        reject(false);
      });
    } else {
      reject(false);
    }
  });
}


function getUserInfoServer() {
  return new Promise(function (resolve, reject) {

    util.request(api.userInfoUrl, {

    }, 'GET').then(res => {
      if (res.errno === 0) {
        //存储用户信息
        wx.setStorageSync('userInfo', res.data);

        resolve(res);
      } else {
        reject(res);
      }
    }).catch((err) => {
      reject(err);
    });

  });
}


/**
 * 获取登录信息
 */
function getLoginUserInfo() {

  if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
    return wx.getStorageSync('userInfo');
  } else {
    return null;
  }

}


/**
 * 存最近一次访问的 机构ID
 * @param {} params 
 */
function setLatestOrg(orgId) {
  //存储用户信息
  if (null != orgId && orgId != '') {
    wx.setStorageSync('latestOrgId', orgId);
  }

}


/**
 * 获取最近一次访问的 机构ID
 */
function getLatestOrg() {
  let orgId = wx.getStorageSync('latestOrgId');
  if (orgId != null && orgId != '') {
    return orgId;
  } else {
    return null;
  }

}

function getLoginToken() {
  return wx.getStorageSync('token')
}


module.exports = {
  loginByWeixin,
  checkLogin,
  getLoginUserInfo,
  getUserInfoServer,
  setLatestOrg,
  getLatestOrg,
  getLoginToken,
};